package cn.sirun.com.fca.account.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.CaptchaLoader;
import cn.sirun.com.fca.account.model.base.ResendResponse;
import cn.sirun.com.fca.account.model.base.SRPEncryptInfo;
import cn.sirun.com.fca.account.model.base.SubmitPhoneRequest;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.model.register.RegisterSubmitResponse;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.ICaptchaPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class CaptchaPresenter extends AccountFlowPresenter<CaptchaLoader> implements ICaptchaPresenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    public void submitCaptcha(String captcha) {
        getLoader().regSubmitCaptcha(captcha)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<RegisterSubmitResponse>>() {

                    @Override
                    public void onNext(BaseResponse<RegisterSubmitResponse> loginResponse) {
                        super.onNext(loginResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_REG_SUBMIT_CAPTCHA_RESULT, loginResponse);
                    }
                });
    }

    @Override
    public void resendCaptcha() {
        getLoader().resendCaptcha()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ResendResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ResendResponse> resendResponse) {
                        super.onNext(resendResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_RESEND_CAPTCHA_RESPONSE, resendResponse);
                    }
                });
    }

    @Override
    public void submitEmialCaptcha(String captcha) {
        getLoader().regSubmitEmialCaptcha(captcha)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<RegisterSubmitResponse>>() {

                    @Override
                    public void onNext(BaseResponse<RegisterSubmitResponse> loginResponse) {
                        super.onNext(loginResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_REG_SUBMIT_CAPTCHA_RESULT, loginResponse);
                    }
                });
    }

    @Override
    public void resendEmailCaptcha() {
        getLoader().resendEmailCaptcha()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ResendResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ResendResponse> resendResponse) {
                        super.onNext(resendResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_RESEND_CAPTCHA_RESPONSE, resendResponse);
                    }
                });
    }

    @Override
    public void resetSubmitCaptcha(String captcha) {
        getLoader().resetSubmitCaptcha(captcha)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SRPEncryptInfo>>() {

                    @Override
                    public void onNext(BaseResponse<SRPEncryptInfo> loginResponse) {
                        super.onNext(loginResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_RESET_SUBMIT_CAPTCHA_RESULT, loginResponse);
                    }
                });
    }

    @Override
    public void resetResendCaptcha() {
        getLoader().resetResendCaptcha()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ResendResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ResendResponse> resendResponse) {
                        super.onNext(resendResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_RESEND_CAPTCHA_RESPONSE, resendResponse);
                    }
                });
    }

    @Override
    public void resetEmailSubmitCaptcha(String captcha) {
        getLoader().resetSubmitEmialCaptcha(captcha)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<SRPEncryptInfo>>() {

                    @Override
                    public void onNext(BaseResponse<SRPEncryptInfo> loginResponse) {
                        super.onNext(loginResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_RESET_SUBMIT_CAPTCHA_RESULT, loginResponse);
                    }
                });
    }

    @Override
    public void resetEmailResendCaptcha() {
        getLoader().resetEmailResendCaptcha()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<ResendResponse>>() {
                    @Override
                    public void onNext(BaseResponse<ResendResponse> resendResponse) {
                        super.onNext(resendResponse);
                        CaptchaPresenter.this.getActionListener().onAction(AccountActionConst
                                .Normal.ACTION_RESEND_CAPTCHA_RESPONSE, resendResponse);
                    }
                });
    }

    @Override
    protected CaptchaLoader createLoader() {
        return new CaptchaLoader();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
